package cn.edu.hznu.util.socket.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
	
	public static void main(String[] args) {
		String strSend = "Hello UDPclient";
		byte[] buf = new byte[1024];
		try {
			DatagramSocket ds = new DatagramSocket(3000);
			DatagramPacket dpReceive = new DatagramPacket(buf, 1024);
			System.out.println("Server is on, waiting for client to send data");
			boolean f = true;
			while (f) {
				ds.receive(dpReceive);
				System.out.println("server received data from client: ");
				String strReceive = new String(dpReceive.getData(), 0, dpReceive.getLength())
						+ " from " + dpReceive.getAddress().getHostAddress() + ": " + dpReceive.getPort();
				System.out.println(strReceive);
				DatagramPacket dpSend = new DatagramPacket(strSend.getBytes(), strSend.length(), dpReceive.getAddress(), 9000);
				ds.send(dpSend);
				dpReceive.setLength(1024);
			}
			ds.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
